import os,sys
sys.path.append(os.getcwd())
os.environ['QT_QPA_PLATFORM'] = 'wayland'

import cv2 as cv
import numpy as np
from Drivers import camera
from ComputerVision import commonProcess

def null_function(args):
    pass
if __name__ == '__main__':
    camera.picap.set_controls({"ExposureTime":20000})
    camera.openCamera()
    cv.namedWindow('preview')
    cv.namedWindow('getThreshould')
    cv.createTrackbar("lowerH","getThreshould",0,179,null_function)
    cv.createTrackbar("upperH","getThreshould",179,179,null_function)
    cv.createTrackbar("lowerS","getThreshould",0,255,null_function)
    cv.createTrackbar("upperS","getThreshould",255,255,null_function)
    cv.createTrackbar("lowerV","getThreshould",0,255,null_function)
    cv.createTrackbar("upperV","getThreshould",255,255,null_function)
        
    while True:
        image = camera.captureImage()
        image = commonProcess.cropToRequirement(image)
        lowerH = cv.getTrackbarPos('lowerH','getThreshould')
        lowerS = cv.getTrackbarPos('lowerS','getThreshould')
        lowerV = cv.getTrackbarPos('lowerV','getThreshould')
        upperH = cv.getTrackbarPos('upperH','getThreshould')
        upperS = cv.getTrackbarPos('upperS','getThreshould')
        upperV = cv.getTrackbarPos('upperV','getThreshould')

        lowerHSV = np.array([lowerH,lowerS,lowerV])
        upperHSV = np.array([upperH,upperS,upperV])

        imageHSV = cv.cvtColor(image,cv.COLOR_BGR2HSV)
        imageMask = cv.inRange(imageHSV,lowerHSV,upperHSV)
        cv.imshow('preview',imageMask)
        k = cv.waitKey(1)
        if k == ord('q'):
            break